#include "ObBlackground.h"
#include "D3DApp.h"
#include "Camera.h"

IDirect3DTexture9* ObBlackGround::mTex = 0;

ObBlackGround::ObBlackGround(){
	setId(S10);
	mInfo	= 0;
	mSpritePos.x = 8.0f;
	mSpritePos.y = 8.0f;
}

ObBlackGround::ObBlackGround(int id){
	setId(id);
	mInfo	= 0;
	mSpritePos.x = 8.0f;
	mSpritePos.y = 8.0f;
}

ObBlackGround::~ObBlackGround(){

}

bool ObBlackGround::setId(int nid){
	if(checkType(nid)){
		mID = nid;
		mWidth	= 16.0f;
		mHeight = 16.0f;
		switch (mID)
		{
		case S10:
			mScale = 10.0f;
			break;
		case S20:
			mScale = 20.0f;
			break;
		case S40:
			mScale = 40.0f;
			break;
		case S80:
			mScale = 80.0f;
			break;
		default:
			mScale = 10.0f;
			break;
		}
		mCenter = D3DXVECTOR3(mWidth/2,mHeight/2,0.0f);
		return true;
	}else{
		return false;
	}
}

bool ObBlackGround::checkType(int obID){
	if(obID<S10 || obID>S80){
		return false;
	}else{
		return true;
	}
}

void ObBlackGround::update(float dt){
	//mRotation+=dt;
}

void ObBlackGround::render(ID3DXSprite* mSprite){
	if(mTex == 0) return;

	D3DXMATRIX S;
	D3DXMatrixScaling(&S,mScale,-mScale, 0);
	D3DXMATRIX R;
	D3DXMatrixRotationZ(&R, mRotation);
	D3DXMATRIX T;
	D3DXMatrixTranslation(&T,mPos.x - gCamera->getPos().x,mPos.y - gCamera->getPos().y,0);
	mSprite->SetTransform(&(S*R*T));
	RECT rect = calRenderRECT(mSpritePos,mWidth,mHeight);
	mSprite->Draw(mTex,&rect,&mCenter,0,D3DCOLOR_XRGB(255, 255, 255)); //RECT : rect in text, CENTER : center of width height
	mSprite->Flush();
}

bool ObBlackGround::loadTexture(){
	if(mTex == 0){
		D3DXCreateTextureFromFileEx(gDevice,"Sprites/Blackground.png",
			D3DX_DEFAULT_NONPOW2,
			D3DX_DEFAULT_NONPOW2,//D3DX_DEFAULT_NONPOW2
			0,
			0,
			D3DFMT_UNKNOWN,
			D3DPOOL_MANAGED,
			D3DX_DEFAULT,
			D3DX_DEFAULT,
			D3DCOLOR_XRGB(0,0,0),
			NULL,
			NULL,
			&mTex);
		return true;
	}else{
		return false;
	}
}

bool ObBlackGround::releaseTexture(){
	if(mTex != 0){
		ReleaseCOM(mTex);
		return true;
	}else{
		return false;
	}
}
